package user

import (
	"d-serv-go/server/core/bean"
	"d-serv-go/server/core/config"
	"d-serv-go/server/core/consts"
	"d-serv-go/server/core/db"
	"d-serv-go/server/core/local"
	"d-serv-go/server/core/serv"
	"d-serv-go/server/core/user/login"
)

type LoginServ struct {
	serv.CommServ
}

type LoginModule interface {
	Login(ParamBean *db.ParamBean) *bean.OutBean
}

func (l *LoginServ) Login(paramBean *db.ParamBean) *bean.OutBean {
	module := config.Instance.LoginModule
	out := l.getLoginModule(module).Login(paramBean)
	return out
}

func (l *LoginServ) getLoginModule(name string) LoginModule {
	switch name {
	case "ldap":
		return &login.LdapModule{}
	case "oauth":
		return &login.OauthModule{}
	default:
		return &login.PasswordModule{}
	}
}

func (l *LoginServ) Logout(paramBean *db.ParamBean) *bean.OutBean {
	outBean := bean.NewOutBean()
	userBean := db.Ctx().GetUserBean()
	if nil != userBean {
		if token, ok := local.Get(consts.SESSIONID).(string); ok {
			db.UserMgr.RemoveTokenUserStateCache(token)
			// RequestUtils.removeSession(Context.getRequest()); // 清除session，自动处理在线信息

			outBean.Set("USER_CODE", userBean.GetCode())
			outBean.Set("USER_TOKEN", token)
		}

		// VertxMgr.vertx.eventBus().send(VertxEvent.CACHE_USER_USERSTATE, new JsonObject().mapFrom(userBean).put("act", "logout"));
	}
	outBean.SetOk("")
	return outBean
}
